package com.masterclient.app.modules.aftersale;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ListView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselibrary.bean.aftersale.ShipmentsBean;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.address.SortAdapter;
import com.masterclient.app.widget.SlideBar2;
import com.masterclient.app.widget.TitlebarView;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/9/22.
 */
@Route(path = AroutePath.LOGISTICS_COMPANY_ACTIVITY)
public class LogisticsCompanyActivity extends BaseActivity {

    @BindView(R.id.logistics_company_bar)
    TitlebarView logisticsCompanyBar;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.side_bar)
    SlideBar2 sideBar;

    private List<ShipmentsBean.DataBean.ShShipmentCompanysBean> listCompany;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_company);
        ButterKnife.bind(this);

        listCompany = (List<ShipmentsBean.DataBean.ShShipmentCompanysBean>) getIntent().getSerializableExtra("list");

        logisticsCompanyBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        initView();
        initData();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
        sideBar = (SlideBar2) findViewById(R.id.side_bar);
        sideBar.setOnStrSelectCallBack((index, selectStr) -> {
            for (int i = 0; i < listCompany.size(); i++) {
                if (selectStr.equalsIgnoreCase(listCompany.get(i).getFirstLetter())) {
                    listView.setSelection(i); // 选择到首字母出现的位置
                    return;
                }
            }
        });
    }

    private void initData() {
        for (int i = 0; i <listCompany.size();i++){
            listCompany.get(i).setSh_name(listCompany.get(i).getSh_name()); // 亳[bó]属于不常见的二级汉字
        }
        Collections.sort(listCompany); // 对list进行排序，需要让User实现Comparable接口重写compareTo方法
        SortAdapter adapter = new SortAdapter(this, listCompany);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent();
            intent.putExtra("bean",listCompany.get(position));
            setResult(RESULT_OK,intent);
            finish();
        });
    }
}
